/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * File: LBG_Arctan_FOC.c
 *
 * Code generated for Simulink model 'LBG_Arctan_FOC'.
 *
 * Model version                  : 2.81
 * Simulink Coder version         : 9.8 (R2022b) 13-May-2022
 * C/C++ source code generated on : Tue May 13 13:09:21 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "LBG_Arctan_FOC.h"
#include "rtwtypes.h"
#include "LBG_Arctan_FOC_private.h"
#include <math.h>
#include "rt_nonfinite.h"
#include <float.h>
#include "rt_defines.h"

/* Named constants for Chart: '<S2>/Chart' */
#define LBG_Arctan_FOC_IN_CLL          ((uint8_T)1U)
#define LBG_Arctan_FOC_IN_CLL1         ((uint8_T)2U)
#define LBG_Arctan_FOC_IN_Init         ((uint8_T)3U)
#define LBG_Arctan_FOC_IN_OPL          ((uint8_T)4U)
#define LBG_Arctan_FOC_IN_OPL1         ((uint8_T)5U)

/* Exported data definition */

/* Definition for custom storage class: Struct */
rt_Simulink_Struct_type rt_Simulink_Struct;

/* Block signals (default storage) */
B_LBG_Arctan_FOC_T LBG_Arctan_FOC_B;

/* Block states (default storage) */
DW_LBG_Arctan_FOC_T LBG_Arctan_FOC_DW;

/* Real-time model */
static RT_MODEL_LBG_Arctan_FOC_T LBG_Arctan_FOC_M_;
RT_MODEL_LBG_Arctan_FOC_T *const LBG_Arctan_FOC_M = &LBG_Arctan_FOC_M_;
static void rate_scheduler(void);

/*
 *         This function updates active task flag for each subrate.
 *         The function is called at model base rate, hence the
 *         generated code self-manages all its subrates.
 */
static void rate_scheduler(void)
{
  /* Compute which subrates run during the next base time step.  Subrates
   * are an integer multiple of the base rate counter.  Therefore, the subtask
   * counter is reset when it reaches its limit (zero means run).
   */
  (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[1])++;
  if ((LBG_Arctan_FOC_M->Timing.TaskCounters.TID[1]) > 19) {/* Sample time: [0.001s, 0.0s] */
    LBG_Arctan_FOC_M->Timing.TaskCounters.TID[1] = 0;
  }

  (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[2])++;
  if ((LBG_Arctan_FOC_M->Timing.TaskCounters.TID[2]) > 199) {/* Sample time: [0.01s, 0.0s] */
    LBG_Arctan_FOC_M->Timing.TaskCounters.TID[2] = 0;
  }
}

/*
 * Output and update for enable system:
 *    '<S17>/per Uint'
 *    '<S41>/per Uint'
 */
void LBG_Arctan_FOC_perUint(boolean_T rtu_Enable, real32_T rtu_In1, real32_T
  *rty_Out1)
{
  /* Outputs for Enabled SubSystem: '<S17>/per Uint' incorporates:
   *  EnablePort: '<S29>/Enable'
   */
  if (rtu_Enable) {
    real32_T rtb_Gain_p4;

    /* Gain: '<S29>/Gain' */
    rtb_Gain_p4 = 0.159154937F * rtu_In1;

    /* Switch: '<S29>/Switch' incorporates:
     *  Bias: '<S29>/Bias'
     */
    if (rtb_Gain_p4 >= 0.0F) {
      *rty_Out1 = rtb_Gain_p4;
    } else {
      *rty_Out1 = rtb_Gain_p4 + 1.0F;
    }

    /* End of Switch: '<S29>/Switch' */
  }

  /* End of Outputs for SubSystem: '<S17>/per Uint' */
}

/*
 * Output and update for atomic system:
 *    '<S31>/MATLAB cos'
 *    '<S33>/MATLAB cos'
 */
void LBG_Arctan_FOC_MATLABcos(real32_T rtu_u, real32_T *rty_y)
{
  *rty_y = rtu_u;
  *rty_y = cosf(*rty_y);
}

/*
 * Output and update for atomic system:
 *    '<S31>/MATLAB sin'
 *    '<S33>/MATLAB sin'
 */
void LBG_Arctan_FOC_MATLABsin(real32_T rtu_u, real32_T *rty_y)
{
  *rty_y = rtu_u;
  *rty_y = sinf(*rty_y);
}

real32_T rt_modf_snf(real32_T u0, real32_T u1)
{
  real32_T y;
  y = u0;
  if (u1 == 0.0F) {
    if (u0 == 0.0F) {
      y = u1;
    }
  } else if (rtIsNaNF(u0) || rtIsNaNF(u1) || rtIsInfF(u0)) {
    y = (rtNaNF);
  } else if (u0 == 0.0F) {
    y = 0.0F / u1;
  } else if (rtIsInfF(u1)) {
    if ((u1 < 0.0F) != (u0 < 0.0F)) {
      y = u1;
    }
  } else {
    boolean_T yEq;
    y = fmodf(u0, u1);
    yEq = (y == 0.0F);
    if ((!yEq) && (u1 > floorf(u1))) {
      real32_T q;
      q = fabsf(u0 / u1);
      yEq = !(fabsf(q - floorf(q + 0.5F)) > FLT_EPSILON * q);
    }

    if (yEq) {
      y = u1 * 0.0F;
    } else if ((u0 < 0.0F) != (u1 < 0.0F)) {
      y += u1;
    }
  }

  return y;
}

real32_T rt_atan2f_snf(real32_T u0, real32_T u1)
{
  real32_T y;
  if (rtIsNaNF(u0) || rtIsNaNF(u1)) {
    y = (rtNaNF);
  } else if (rtIsInfF(u0) && rtIsInfF(u1)) {
    int32_T tmp;
    int32_T tmp_0;
    if (u0 > 0.0F) {
      tmp = 1;
    } else {
      tmp = -1;
    }

    if (u1 > 0.0F) {
      tmp_0 = 1;
    } else {
      tmp_0 = -1;
    }

    y = atan2f((real32_T)tmp, (real32_T)tmp_0);
  } else if (u1 == 0.0F) {
    if (u0 > 0.0F) {
      y = RT_PIF / 2.0F;
    } else if (u0 < 0.0F) {
      y = -(RT_PIF / 2.0F);
    } else {
      y = 0.0F;
    }
  } else {
    y = atan2f(u0, u1);
  }

  return y;
}

/* Model step function */
void LBG_Arctan_FOC_step(real32_T arg_Target_Speed, real32_T arg_Iabc[3],
  real32_T arg_Real_Theta, real32_T arg_Vabc_PU[3])
{
  real32_T T1;
  real32_T T2;
  real32_T beta;
  real32_T rtb_Saturation;
  real32_T rtb_Saturation_a;
  real32_T rtb_Saturation_h;
  real32_T rtb_Saturation_l;
  real32_T rtb_Sum2;
  real32_T rtb_Sum3;
  real32_T rtb_Sum_b;
  real32_T rtb_Tcmp3;
  real32_T rtb_Vbeta;
  real32_T rtb_algDD_o1_m;
  real32_T rtb_ibeta;
  real32_T rtb_sum_alpha;
  real32_T ta;
  uint32_T elapsedTicks;
  uint16_T rtb_Get_Integer;
  int8_T rtPrevAction;
  int8_T tmp;
  UNUSED_PARAMETER(arg_Real_Theta);
  if (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[2] == 0) {
    /* S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
     *  SubSystem: '<S1>/10ms_Task'
     */
    /* Chart: '<S2>/Chart' incorporates:
     *  Inport: '<Root>/Target_Speed'
     */
    elapsedTicks = LBG_Arctan_FOC_M->Timing.clockTick2 -
      LBG_Arctan_FOC_DW.previousTicks;
    LBG_Arctan_FOC_DW.previousTicks = LBG_Arctan_FOC_M->Timing.clockTick2;
    if (LBG_Arctan_FOC_DW.temporalCounter_i1 + elapsedTicks <= 63U) {
      LBG_Arctan_FOC_DW.temporalCounter_i1 = (uint8_T)
        (LBG_Arctan_FOC_DW.temporalCounter_i1 + elapsedTicks);
    } else {
      LBG_Arctan_FOC_DW.temporalCounter_i1 = 63U;
    }

    if (LBG_Arctan_FOC_DW.is_active_c1_LBG_Arctan_FOC == 0U) {
      LBG_Arctan_FOC_DW.is_active_c1_LBG_Arctan_FOC = 1U;
      LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_Init;
      LBG_Arctan_FOC_DW.temporalCounter_i1 = 0U;
      rt_Simulink_Struct.Motor_state = 0;
    } else {
      switch (LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC) {
       case LBG_Arctan_FOC_IN_CLL:
        rt_Simulink_Struct.Motor_state = 2;
        LBG_Arctan_FOC_DW.time = 0.0;
        break;

       case LBG_Arctan_FOC_IN_CLL1:
        if (arg_Target_Speed > 0.0F) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_Init;
          LBG_Arctan_FOC_DW.temporalCounter_i1 = 0U;
          rt_Simulink_Struct.Motor_state = 0;
        } else {
          rt_Simulink_Struct.Motor_state = 2;
          LBG_Arctan_FOC_DW.time = 0.0;
        }
        break;

       case LBG_Arctan_FOC_IN_Init:
        if ((LBG_Arctan_FOC_DW.temporalCounter_i1 >= 50U) && (arg_Target_Speed >=
             0.0F)) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_OPL;
          rt_Simulink_Struct.Motor_state = 1;
          LBG_Arctan_FOC_DW.time++;
        } else if ((LBG_Arctan_FOC_DW.temporalCounter_i1 >= 50U) &&
                   (arg_Target_Speed < 0.0F)) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_OPL1;
          rt_Simulink_Struct.Motor_state = 3;
          LBG_Arctan_FOC_DW.time++;
        } else {
          rt_Simulink_Struct.Motor_state = 0;
        }
        break;

       case LBG_Arctan_FOC_IN_OPL:
        if ((LBG_Arctan_FOC_DW.time > 100.0) && (LBG_Arctan_FOC_DW.Delay_DSTATE >
             900.0F)) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_CLL;
          rt_Simulink_Struct.Motor_state = 2;
          LBG_Arctan_FOC_DW.time = 0.0;
        } else {
          rt_Simulink_Struct.Motor_state = 1;
          LBG_Arctan_FOC_DW.time++;
        }
        break;

       default:
        /* case IN_OPL1: */
        if ((LBG_Arctan_FOC_DW.time > 100.0) && (LBG_Arctan_FOC_DW.Delay_DSTATE <
             -900.0F)) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_CLL1;
          rt_Simulink_Struct.Motor_state = 2;
          LBG_Arctan_FOC_DW.time = 0.0;
        } else {
          rt_Simulink_Struct.Motor_state = 3;
          LBG_Arctan_FOC_DW.time++;
        }
        break;
      }
    }

    /* End of Chart: '<S2>/Chart' */
    /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator5' */
  }

  if (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[1] == 0) {
    /* S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
     *  SubSystem: '<S1>/Speed_Control'
     */
    /* Outputs for Enabled SubSystem: '<S4>/Subsystem' incorporates:
     *  EnablePort: '<S389>/Enable'
     */
    /* RelationalOperator: '<S388>/Compare' incorporates:
     *  Constant: '<S388>/Constant'
     */
    if (rt_Simulink_Struct.Motor_state == 2) {
      if (!LBG_Arctan_FOC_DW.Subsystem_MODE) {
        LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = true;

        /* Enable for DiscreteIntegrator: '<S422>/Integrator' */
        LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 1U;
        LBG_Arctan_FOC_DW.Subsystem_MODE = true;
      }

      if (LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T) {
        elapsedTicks = 0U;
      } else {
        elapsedTicks = LBG_Arctan_FOC_M->Timing.clockTick1 -
          LBG_Arctan_FOC_DW.Subsystem_PREV_T;
      }

      LBG_Arctan_FOC_DW.Subsystem_PREV_T = LBG_Arctan_FOC_M->Timing.clockTick1;
      LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = false;

      /* SignalConversion generated from: '<S389>/Id_Ref' */
      LBG_Arctan_FOC_B.OutportBufferForId_Ref = LBG_Arctan_FOC_ConstB.Constant;

      /* Sum: '<S389>/Sum' incorporates:
       *  Inport: '<Root>/Target_Speed'
       */
      rtb_Sum_b = arg_Target_Speed - LBG_Arctan_FOC_DW.Delay_DSTATE;

      /* DiscreteIntegrator: '<S422>/Integrator' */
      if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h == 0) {
        /* DiscreteIntegrator: '<S422>/Integrator' */
        LBG_Arctan_FOC_DW.Integrator_DSTATE_cq += 0.001F * (real32_T)
          elapsedTicks * LBG_Arctan_FOC_DW.Integrator_PREV_U_a;
      }

      /* End of DiscreteIntegrator: '<S422>/Integrator' */

      /* Sum: '<S431>/Sum' incorporates:
       *  Gain: '<S427>/Proportional Gain'
       */
      LBG_Arctan_FOC_B.Saturation = 4.59839575E-5F * rtb_Sum_b +
        LBG_Arctan_FOC_DW.Integrator_DSTATE_cq;

      /* Saturate: '<S429>/Saturation' */
      if (LBG_Arctan_FOC_B.Saturation > 3.26732683F) {
        /* Sum: '<S431>/Sum' incorporates:
         *  Saturate: '<S429>/Saturation'
         */
        LBG_Arctan_FOC_B.Saturation = 3.26732683F;
      } else if (LBG_Arctan_FOC_B.Saturation < -3.26732683F) {
        /* Sum: '<S431>/Sum' incorporates:
         *  Saturate: '<S429>/Saturation'
         */
        LBG_Arctan_FOC_B.Saturation = -3.26732683F;
      }

      /* End of Saturate: '<S429>/Saturation' */

      /* Update for DiscreteIntegrator: '<S422>/Integrator' incorporates:
       *  Gain: '<S419>/Integral Gain'
       */
      LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 0U;
      LBG_Arctan_FOC_DW.Integrator_PREV_U_a = 0.000727070204F * rtb_Sum_b;
    } else {
      LBG_Arctan_FOC_DW.Subsystem_MODE = false;
    }

    /* End of RelationalOperator: '<S388>/Compare' */
    /* End of Outputs for SubSystem: '<S4>/Subsystem' */
    /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator3' */
  }

  /* S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  if (LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T) {
    elapsedTicks = 0U;
  } else {
    elapsedTicks = LBG_Arctan_FOC_M->Timing.clockTick0 -
      LBG_Arctan_FOC_DW.FOC_PREV_T;
  }

  /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem3' incorporates:
   *  ActionPort: '<S387>/Action Port'
   */
  /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
   *  ActionPort: '<S385>/Action Port'
   */
  /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem3' incorporates:
   *  ActionPort: '<S387>/Action Port'
   */
  /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
   *  ActionPort: '<S385>/Action Port'
   */
  /* SwitchCase: '<S9>/Switch Case' */
  LBG_Arctan_FOC_DW.FOC_PREV_T = LBG_Arctan_FOC_M->Timing.clockTick0;

  /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem1' */
  /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem3' */
  /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem1' */
  /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem3' */
  LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T = false;

  /* MATLAB Function: '<S7>/Clark2' incorporates:
   *  Inport: '<Root>/Iabc'
   */
  rtb_Sum_b = arg_Iabc[0] * 2.0F / 3.0F - (arg_Iabc[1] + arg_Iabc[2]) / 3.0F;
  rtb_ibeta = (arg_Iabc[1] - arg_Iabc[2]) * 1.73205078F / 3.0F;

  /* Saturate: '<S10>/Saturation' incorporates:
   *  Delay: '<S10>/Delay'
   */
  if (LBG_Arctan_FOC_DW.Delay_DSTATE_n > 3000.0F) {
    rtb_sum_alpha = 3000.0F;
  } else if (LBG_Arctan_FOC_DW.Delay_DSTATE_n < 10.0F) {
    rtb_sum_alpha = 10.0F;
  } else {
    rtb_sum_alpha = LBG_Arctan_FOC_DW.Delay_DSTATE_n;
  }

  /* End of Saturate: '<S10>/Saturation' */

  /* SwitchCase: '<S9>/Switch Case' */
  rtPrevAction = LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem;
  LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = -1;
  switch (rt_Simulink_Struct.Motor_state) {
   case 0:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 0;
    break;

   case 1:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 1;
    break;

   case 2:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 2;
    break;

   case 3:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 3;
    break;
  }

  switch (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem) {
   case 0:
    /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem' incorporates:
     *  ActionPort: '<S384>/Action Port'
     */
    /* Merge: '<S9>/Merge' incorporates:
     *  Constant: '<S384>/Constant1'
     *  SignalConversion generated from: '<S384>/Id_Ref'
     */
    rt_Simulink_Struct.cal_idref = 0.4F;

    /* Merge: '<S9>/Merge1' incorporates:
     *  Constant: '<S384>/Constant'
     *  SignalConversion generated from: '<S384>/Iq_Ref'
     */
    rt_Simulink_Struct.cal_iqref = 0.0F;

    /* Merge: '<S9>/Merge2' incorporates:
     *  Constant: '<S384>/Constant2'
     *  SignalConversion generated from: '<S384>/Theta'
     */
    rt_Simulink_Struct.cal_theta = 0.0F;

    /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem' */
    break;

   case 1:
    {
      uint32_T SwitchCaseActionSubsystem1_ELAP;
      if (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem != rtPrevAction) {
        LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = true;

        /* Enable for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
         *  ActionPort: '<S385>/Action Port'
         */
        /* Enable for SwitchCase: '<S9>/Switch Case' incorporates:
         *  DiscreteIntegrator: '<S385>/Discrete-Time Integrator'
         *  DiscreteIntegrator: '<S385>/Discrete-Time Integrator1'
         */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTE_m = 1U;
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_n = 1U;

        /* End of Enable for SubSystem: '<S9>/Switch Case Action Subsystem1' */
      }

      /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
       *  ActionPort: '<S385>/Action Port'
       */
      if (LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE) {
        SwitchCaseActionSubsystem1_ELAP = 0U;
      } else {
        SwitchCaseActionSubsystem1_ELAP = LBG_Arctan_FOC_DW.FOC_PREV_T
          - LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV;
      }

      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV =
        LBG_Arctan_FOC_DW.FOC_PREV_T;
      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = false;

      /* DiscreteIntegrator: '<S385>/Discrete-Time Integrator1' */
      if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTE_m == 0) {
        /* DiscreteIntegrator: '<S385>/Discrete-Time Integrator1' */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTAT_l += 5.0E-5F * (real32_T)
          SwitchCaseActionSubsystem1_ELAP *
          LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV__o;
      }

      /* End of DiscreteIntegrator: '<S385>/Discrete-Time Integrator1' */

      /* Merge: '<S9>/Merge' incorporates:
       *  Constant: '<S385>/Constant1'
       *  SignalConversion generated from: '<S385>/Id_Ref'
       */
      rt_Simulink_Struct.cal_idref = 0.4F;

      /* Merge: '<S9>/Merge1' incorporates:
       *  Constant: '<S385>/Constant'
       *  SignalConversion generated from: '<S385>/Iq_Ref'
       */
      rt_Simulink_Struct.cal_iqref = 0.0F;

      /* DiscreteIntegrator: '<S385>/Discrete-Time Integrator' */
      if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_n == 0) {
        /* DiscreteIntegrator: '<S385>/Discrete-Time Integrator' */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_e += 5.0E-5F * (real32_T)
          SwitchCaseActionSubsystem1_ELAP *
          LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV__bg;
      }

      /* End of DiscreteIntegrator: '<S385>/Discrete-Time Integrator' */

      /* Merge: '<S9>/Merge2' incorporates:
       *  Constant: '<S385>/Constant4'
       *  Math: '<S385>/Math Function'
       *  SignalConversion generated from: '<S385>/Theta'
       */
      rt_Simulink_Struct.cal_theta = rt_modf_snf
        (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTAT_l, 6.28318548F);

      /* Update for DiscreteIntegrator: '<S385>/Discrete-Time Integrator1' incorporates:
       *  Gain: '<S385>/Gain'
       *  Gain: '<S385>/Gain1'
       */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTE_m = 0U;
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV__o = 0.116666667F *
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_e * 6.28318548F;

      /* Update for DiscreteIntegrator: '<S385>/Discrete-Time Integrator' */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_n = 0U;
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV__bg =
        LBG_Arctan_FOC_ConstB.Divide_d;

      /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem1' */
    }
    break;

   case 2:
    /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem2' incorporates:
     *  ActionPort: '<S386>/Action Port'
     */
    /* Merge: '<S9>/Merge' incorporates:
     *  SignalConversion generated from: '<S386>/Id'
     */
    rt_Simulink_Struct.cal_idref = LBG_Arctan_FOC_B.OutportBufferForId_Ref;

    /* Merge: '<S9>/Merge1' incorporates:
     *  SignalConversion generated from: '<S386>/Iq'
     */
    rt_Simulink_Struct.cal_iqref = LBG_Arctan_FOC_B.Saturation;

    /* Merge: '<S9>/Merge2' incorporates:
     *  Delay: '<S3>/Delay'
     *  SignalConversion generated from: '<S386>/Obs_Theta'
     */
    rt_Simulink_Struct.cal_theta = LBG_Arctan_FOC_DW.Delay_DSTATE_o;

    /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem2' */
    break;

   case 3:
    {
      uint32_T SwitchCaseActionSubsystem1_ELAP;
      if (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem != rtPrevAction) {
        LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_RESE = true;

        /* Enable for IfAction SubSystem: '<S9>/Switch Case Action Subsystem3' incorporates:
         *  ActionPort: '<S387>/Action Port'
         */
        /* Enable for SwitchCase: '<S9>/Switch Case' incorporates:
         *  DiscreteIntegrator: '<S387>/Discrete-Time Integrator'
         *  DiscreteIntegrator: '<S387>/Discrete-Time Integrator1'
         */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ = 1U;
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTE_fz = 1U;

        /* End of Enable for SubSystem: '<S9>/Switch Case Action Subsystem3' */
      }

      /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem3' incorporates:
       *  ActionPort: '<S387>/Action Port'
       */
      if (LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_RESE) {
        SwitchCaseActionSubsystem1_ELAP = 0U;
      } else {
        SwitchCaseActionSubsystem1_ELAP = LBG_Arctan_FOC_DW.FOC_PREV_T
          - LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_PREV;
      }

      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_PREV =
        LBG_Arctan_FOC_DW.FOC_PREV_T;
      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_RESE = false;

      /* DiscreteIntegrator: '<S387>/Discrete-Time Integrator1' */
      if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ == 0) {
        /* DiscreteIntegrator: '<S387>/Discrete-Time Integrator1' */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTATE += 5.0E-5F * (real32_T)
          SwitchCaseActionSubsystem1_ELAP *
          LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV_U;
      }

      /* End of DiscreteIntegrator: '<S387>/Discrete-Time Integrator1' */

      /* Merge: '<S9>/Merge' incorporates:
       *  Constant: '<S387>/Constant1'
       *  SignalConversion generated from: '<S387>/Id_Ref'
       */
      rt_Simulink_Struct.cal_idref = 0.4F;

      /* Merge: '<S9>/Merge1' incorporates:
       *  Constant: '<S387>/Constant'
       *  SignalConversion generated from: '<S387>/Iq_Ref'
       */
      rt_Simulink_Struct.cal_iqref = 0.0F;

      /* DiscreteIntegrator: '<S387>/Discrete-Time Integrator' */
      if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTE_fz == 0) {
        /* DiscreteIntegrator: '<S387>/Discrete-Time Integrator' */
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_o += 5.0E-5F * (real32_T)
          SwitchCaseActionSubsystem1_ELAP *
          LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_b;
      }

      /* End of DiscreteIntegrator: '<S387>/Discrete-Time Integrator' */

      /* Merge: '<S9>/Merge2' incorporates:
       *  Constant: '<S387>/Constant4'
       *  Math: '<S387>/Math Function'
       *  SignalConversion generated from: '<S387>/Theta'
       */
      rt_Simulink_Struct.cal_theta = rt_modf_snf
        (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTATE, 6.28318548F);

      /* Update for DiscreteIntegrator: '<S387>/Discrete-Time Integrator1' incorporates:
       *  Gain: '<S387>/Gain'
       *  Gain: '<S387>/Gain1'
       */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ = 0U;
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV_U = 0.116666667F *
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_o * 6.28318548F;

      /* Update for DiscreteIntegrator: '<S387>/Discrete-Time Integrator' */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTE_fz = 0U;
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_b =
        LBG_Arctan_FOC_ConstB.Divide;

      /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem3' */
    }
    break;
  }

  /* MATLAB Function: '<S7>/Park' */
  rtb_Sum2 = sinf(rt_Simulink_Struct.cal_theta);
  rtb_Sum3 = cosf(rt_Simulink_Struct.cal_theta);
  rt_Simulink_Struct.cal_id_now = rtb_Sum_b * rtb_Sum3 + rtb_ibeta * rtb_Sum2;
  rt_Simulink_Struct.cal_iq_now = -rtb_Sum_b * rtb_Sum2 + rtb_ibeta * rtb_Sum3;

  /* Sum: '<S7>/Sum2' */
  rtb_Sum2 = rt_Simulink_Struct.cal_idref - rt_Simulink_Struct.cal_id_now;

  /* DiscreteIntegrator: '<S301>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE == 0) {
    /* DiscreteIntegrator: '<S301>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U;
  }

  /* End of DiscreteIntegrator: '<S301>/Integrator' */

  /* Sum: '<S310>/Sum' incorporates:
   *  Gain: '<S306>/Proportional Gain'
   */
  rtb_Saturation = 0.485F * rtb_Sum2 + LBG_Arctan_FOC_DW.Integrator_DSTATE;

  /* Saturate: '<S308>/Saturation' */
  if (rtb_Saturation > 6.92820311F) {
    rtb_Saturation = 6.92820311F;
  } else if (rtb_Saturation < -6.92820311F) {
    rtb_Saturation = -6.92820311F;
  }

  /* End of Saturate: '<S308>/Saturation' */

  /* Sum: '<S7>/Sum3' */
  rtb_Sum3 = rt_Simulink_Struct.cal_iqref - rt_Simulink_Struct.cal_iq_now;

  /* DiscreteIntegrator: '<S349>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_e == 0) {
    /* DiscreteIntegrator: '<S349>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_c += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U_c;
  }

  /* End of DiscreteIntegrator: '<S349>/Integrator' */

  /* Sum: '<S358>/Sum' incorporates:
   *  Gain: '<S354>/Proportional Gain'
   */
  rtb_Saturation_l = 0.485F * rtb_Sum3 + LBG_Arctan_FOC_DW.Integrator_DSTATE_c;

  /* Saturate: '<S356>/Saturation' */
  if (rtb_Saturation_l > 6.92820311F) {
    rtb_Saturation_l = 6.92820311F;
  } else if (rtb_Saturation_l < -6.92820311F) {
    rtb_Saturation_l = -6.92820311F;
  }

  /* End of Saturate: '<S356>/Saturation' */

  /* MATLAB Function: '<S7>/svpwm' incorporates:
   *  Constant: '<S7>/Constant'
   *  Constant: '<S7>/Constant1'
   */
  beta = atanf(rtb_Saturation_l / fabsf(rtb_Saturation));
  if (rtb_Saturation * rtb_Saturation + rtb_Saturation_l * rtb_Saturation_l >
      48.0F) {
    rtb_Tcmp3 = cosf(beta);
    rtb_Saturation = -6.92820311F * rtb_Tcmp3;
    rtb_Saturation_l = 6.92820311F * sinf(beta);
    if (rtb_Saturation > 0.0F) {
      rtb_Saturation = 6.92820311F * rtb_Tcmp3;
    }
  }

  rtb_Tcmp3 = sinf(rt_Simulink_Struct.cal_theta);
  rtb_Vbeta = cosf(rt_Simulink_Struct.cal_theta);
  beta = rtb_Saturation * rtb_Vbeta - rtb_Saturation_l * rtb_Tcmp3;
  rtb_Vbeta = rtb_Saturation * rtb_Tcmp3 + rtb_Saturation_l * rtb_Vbeta;
  rt_Simulink_Struct.cal_sector = 0;
  rtb_Saturation = 0.0F;
  rtb_Saturation_l = 0.0F;
  rtb_Tcmp3 = 0.0F;
  if (rtb_Vbeta > 0.0F) {
    rt_Simulink_Struct.cal_sector = 1;
  }

  if ((1.73205078F * beta - rtb_Vbeta) / 2.0F > 0.0F) {
    rt_Simulink_Struct.cal_sector = (int8_T)(rt_Simulink_Struct.cal_sector + 2);
  }

  if ((-1.73205078F * beta - rtb_Vbeta) / 2.0F > 0.0F) {
    rt_Simulink_Struct.cal_sector = (int8_T)(rt_Simulink_Struct.cal_sector + 4);
  }

  switch (rt_Simulink_Struct.cal_sector) {
   case 1:
    T1 = (-1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F;
    T2 = (1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F;
    break;

   case 2:
    T1 = (1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F;
    T2 = -(1.73205078F * rtb_Vbeta * 8192.0F / 12.0F);
    break;

   case 3:
    T1 = -((-1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F);
    T2 = 1.73205078F * rtb_Vbeta * 8192.0F / 12.0F;
    break;

   case 4:
    T1 = -(1.73205078F * rtb_Vbeta * 8192.0F / 12.0F);
    T2 = (-1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F;
    break;

   case 5:
    T1 = 1.73205078F * rtb_Vbeta * 8192.0F / 12.0F;
    T2 = -((1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F);
    break;

   default:
    T1 = -((1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F);
    T2 = -((-1.5F * beta + 0.866025388F * rtb_Vbeta) * 682.666687F);
    break;
  }

  ta = (8192.0F - (T1 + T2)) / 4.0F;
  T1 = T1 / 2.0F + ta;
  T2 = T2 / 2.0F + T1;
  ta = roundf(ta);
  T1 = roundf(T1);
  switch (rt_Simulink_Struct.cal_sector) {
   case 1:
    rtb_Saturation = T1;
    rtb_Saturation_l = ta;
    rtb_Tcmp3 = roundf(T2);
    break;

   case 2:
    rtb_Saturation = ta;
    rtb_Saturation_l = roundf(T2);
    rtb_Tcmp3 = T1;
    break;

   case 3:
    rtb_Saturation = ta;
    rtb_Saturation_l = T1;
    rtb_Tcmp3 = roundf(T2);
    break;

   case 4:
    rtb_Saturation = roundf(T2);
    rtb_Saturation_l = T1;
    rtb_Tcmp3 = ta;
    break;

   case 5:
    rtb_Saturation = roundf(T2);
    rtb_Saturation_l = ta;
    rtb_Tcmp3 = T1;
    break;

   case 6:
    rtb_Saturation = T1;
    rtb_Saturation_l = roundf(T2);
    rtb_Tcmp3 = ta;
    break;
  }

  /* End of MATLAB Function: '<S7>/svpwm' */

  /* Product: '<S12>/Product2' incorporates:
   *  Product: '<S12>/Product8'
   */
  ta = rtb_ibeta * rtb_sum_alpha;

  /* Sum: '<S12>/Add4' incorporates:
   *  Delay: '<S12>/Delay'
   *  Gain: '<S12>/Gain1'
   *  Gain: '<S12>/Gain2'
   *  Gain: '<S12>/Gain3'
   *  Gain: '<S12>/Gain4'
   *  Product: '<S12>/Product'
   *  Product: '<S12>/Product1'
   *  Product: '<S12>/Product2'
   *  Product: '<S12>/Product3'
   *  Sum: '<S12>/Add2'
   *  Sum: '<S12>/Add3'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_a = ((0.1F * rtb_Sum_b - ta * 5.0E-5F) *
    LBG_Arctan_FOC_ConstB.Add1 + LBG_Arctan_FOC_DW.Delay_DSTATE_a *
    LBG_Arctan_FOC_ConstB.Add) + (0.1F * beta - rtb_sum_alpha * rtb_Vbeta *
    5.0E-5F);

  /* Product: '<S12>/Product6' incorporates:
   *  Product: '<S12>/Product9'
   */
  T2 = rtb_Sum_b * rtb_sum_alpha;

  /* Sum: '<S12>/Add9' incorporates:
   *  Delay: '<S12>/Delay1'
   *  Gain: '<S12>/Gain6'
   *  Gain: '<S12>/Gain7'
   *  Gain: '<S12>/Gain8'
   *  Gain: '<S12>/Gain9'
   *  Product: '<S12>/Product4'
   *  Product: '<S12>/Product5'
   *  Product: '<S12>/Product6'
   *  Product: '<S12>/Product7'
   *  Sum: '<S12>/Add7'
   *  Sum: '<S12>/Add8'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE = ((T2 * 5.0E-5F + 0.1F * rtb_ibeta) *
    LBG_Arctan_FOC_ConstB.Add6 + LBG_Arctan_FOC_DW.Delay1_DSTATE *
    LBG_Arctan_FOC_ConstB.Add5) + (rtb_sum_alpha * beta * 5.0E-5F + 0.1F *
    rtb_Vbeta);

  /* Sum: '<S23>/Add1' incorporates:
   *  Constant: '<S23>/One'
   *  Delay: '<S10>/Delay'
   *  Product: '<S23>/Product1'
   *  UnitDelay: '<S23>/Unit Delay'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_n = 0.98F * LBG_Arctan_FOC_DW.UnitDelay_DSTATE
    + LBG_Arctan_FOC_ConstB.Product;

  /* Outputs for Atomic SubSystem: '<S13>/atan2' */
  /* Trigonometry: '<S17>/Atan2' incorporates:
   *  Delay: '<S12>/Delay'
   *  Delay: '<S12>/Delay1'
   *  Gain: '<S12>/Gain10'
   *  Gain: '<S12>/Gain11'
   *  Gain: '<S12>/Gain12'
   *  Gain: '<S12>/Gain13'
   *  Sum: '<S12>/Add10'
   *  Sum: '<S12>/Add11'
   */
  rtb_sum_alpha = rt_atan2f_snf((LBG_Arctan_FOC_DW.Delay_DSTATE_a - 0.194F *
    rtb_Sum_b) + ta * 9.7E-5F, (LBG_Arctan_FOC_DW.Delay1_DSTATE - 0.194F *
    rtb_ibeta) - T2 * 9.7E-5F);

  /* Outputs for Enabled SubSystem: '<S17>/If Action Subsystem' incorporates:
   *  EnablePort: '<S28>/Enable'
   */
  /* Merge: '<S17>/Merge' incorporates:
   *  SignalConversion generated from: '<S28>/In1'
   */
  ta = rtb_sum_alpha;

  /* End of Outputs for SubSystem: '<S17>/If Action Subsystem' */

  /* Outputs for Enabled SubSystem: '<S17>/per Uint' */
  /* RelationalOperator: '<S27>/Compare' incorporates:
   *  Constant: '<S17>/Constant'
   *  Constant: '<S27>/Constant'
   */
  LBG_Arctan_FOC_perUint(false, rtb_sum_alpha, &ta);

  /* End of Outputs for SubSystem: '<S17>/per Uint' */
  /* End of Outputs for SubSystem: '<S13>/atan2' */

  /* Delay: '<S30>/Delay2' */
  rtb_sum_alpha = LBG_Arctan_FOC_DW.Delay2_DSTATE;

  /* Saturate: '<S11>/Saturation' incorporates:
   *  Delay: '<S11>/Delay'
   */
  if (LBG_Arctan_FOC_DW.Delay_DSTATE_g > 14660.7656F) {
    T1 = 14660.7656F;
  } else if (LBG_Arctan_FOC_DW.Delay_DSTATE_g < -14660.7656F) {
    T1 = -14660.7656F;
  } else {
    T1 = LBG_Arctan_FOC_DW.Delay_DSTATE_g;
  }

  /* End of Saturate: '<S11>/Saturation' */

  /* Delay: '<S30>/Delay3' */
  ta = LBG_Arctan_FOC_DW.Delay3_DSTATE;

  /* Sum: '<S30>/Add10' incorporates:
   *  Constant: '<S30>/Constant2'
   *  Delay: '<S30>/Delay'
   *  Delay: '<S30>/Delay2'
   *  Delay: '<S30>/Delay3'
   *  Gain: '<S30>/Gain'
   *  Product: '<S30>/Product1'
   *  Product: '<S30>/Product8'
   *  Sum: '<S30>/Add1'
   */
  LBG_Arctan_FOC_DW.Delay2_DSTATE = (LBG_Arctan_FOC_DW.Delay2_DSTATE - 5.0E-5F *
    T1 * LBG_Arctan_FOC_DW.Delay3_DSTATE) + (LBG_Arctan_FOC_DW.Delay_DSTATE_p -
    rtb_Sum_b) * 1.04244959F;

  /* Sum: '<S30>/Add3' incorporates:
   *  Constant: '<S30>/Constant3'
   *  Delay: '<S30>/Delay1'
   *  Delay: '<S30>/Delay3'
   *  Gain: '<S30>/Gain1'
   *  Product: '<S30>/Product2'
   *  Product: '<S30>/Product3'
   *  Sum: '<S30>/Add2'
   */
  LBG_Arctan_FOC_DW.Delay3_DSTATE = (5.0E-5F * T1 * rtb_sum_alpha +
    LBG_Arctan_FOC_DW.Delay3_DSTATE) + (LBG_Arctan_FOC_DW.Delay1_DSTATE_e -
    rtb_ibeta) * 1.04244959F;

  /* Sum: '<S30>/Add4' incorporates:
   *  Constant: '<S30>/Constant10'
   *  Constant: '<S30>/Constant11'
   *  Constant: '<S30>/Constant9'
   *  Delay: '<S30>/Delay'
   *  Product: '<S30>/Product'
   *  Product: '<S30>/Product10'
   *  Product: '<S30>/Product11'
   *  Product: '<S30>/Product12'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_p = ((LBG_Arctan_FOC_DW.Delay_DSTATE_p *
    LBG_Arctan_FOC_ConstB.Add_p - 0.515463889F * rtb_sum_alpha) + 0.515463889F *
    beta) - -1.36607659F * rtb_Sum_b;

  /* Sum: '<S30>/Add6' incorporates:
   *  Constant: '<S30>/Constant13'
   *  Constant: '<S30>/Constant6'
   *  Constant: '<S30>/Constant7'
   *  Delay: '<S30>/Delay1'
   *  Product: '<S30>/Product4'
   *  Product: '<S30>/Product5'
   *  Product: '<S30>/Product6'
   *  Product: '<S30>/Product7'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE_e = ((LBG_Arctan_FOC_DW.Delay1_DSTATE_e *
    LBG_Arctan_FOC_ConstB.Add5_b - 0.515463889F * ta) + 0.515463889F * rtb_Vbeta)
    - -1.36607659F * rtb_ibeta;

  /* Outputs for Atomic SubSystem: '<S31>/atan2' */
  /* Trigonometry: '<S41>/Atan2' incorporates:
   *  Delay: '<S30>/Delay2'
   *  Delay: '<S30>/Delay3'
   */
  rtb_sum_alpha = rt_atan2f_snf(LBG_Arctan_FOC_DW.Delay2_DSTATE,
    LBG_Arctan_FOC_DW.Delay3_DSTATE);

  /* Outputs for Enabled SubSystem: '<S41>/If Action Subsystem' incorporates:
   *  EnablePort: '<S104>/Enable'
   */
  /* Merge: '<S41>/Merge' incorporates:
   *  SignalConversion generated from: '<S104>/In1'
   */
  LBG_Arctan_FOC_B.Merge_n = rtb_sum_alpha;

  /* End of Outputs for SubSystem: '<S41>/If Action Subsystem' */

  /* Outputs for Enabled SubSystem: '<S41>/per Uint' */
  /* RelationalOperator: '<S103>/Compare' incorporates:
   *  Constant: '<S103>/Constant'
   *  Constant: '<S41>/Constant'
   */
  LBG_Arctan_FOC_perUint(false, rtb_sum_alpha, &LBG_Arctan_FOC_B.Merge_n);

  /* End of Outputs for SubSystem: '<S41>/per Uint' */
  /* End of Outputs for SubSystem: '<S31>/atan2' */

  /* DiscreteIntegrator: '<S31>/Discrete-Time Integrator' */
  if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E == 0) {
    /* DiscreteIntegrator: '<S31>/Discrete-Time Integrator' */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE += 5.0E-5F * (real32_T)
      elapsedTicks * LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U;
  }

  /* End of DiscreteIntegrator: '<S31>/Discrete-Time Integrator' */

  /* Math: '<S31>/Mod' incorporates:
   *  Constant: '<S31>/Constant1'
   */
  T2 = rt_modf_snf(LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE, 6.28318548F);

  /* MATLAB Function: '<S31>/MATLAB cos' */
  LBG_Arctan_FOC_MATLABcos(T2, &ta);

  /* Product: '<S31>/Product4' incorporates:
   *  Delay: '<S30>/Delay2'
   */
  T1 = LBG_Arctan_FOC_DW.Delay2_DSTATE * ta;

  /* Sqrt: '<S31>/Sqrt' incorporates:
   *  Delay: '<S31>/Delay'
   *  Delay: '<S31>/Delay1'
   *  Math: '<S31>/Square'
   *  Math: '<S31>/Square1'
   *  Sum: '<S31>/Add1'
   */
  rtb_sum_alpha = sqrtf(LBG_Arctan_FOC_DW.Delay_DSTATE_l *
                        LBG_Arctan_FOC_DW.Delay_DSTATE_l +
                        LBG_Arctan_FOC_DW.Delay1_DSTATE_j *
                        LBG_Arctan_FOC_DW.Delay1_DSTATE_j);

  /* Saturate: '<S31>/Saturation' */
  if (rtb_sum_alpha > 100.0F) {
    rtb_sum_alpha = 100.0F;
  } else if (rtb_sum_alpha < 0.001F) {
    rtb_sum_alpha = 0.001F;
  }

  /* End of Saturate: '<S31>/Saturation' */

  /* MATLAB Function: '<S31>/MATLAB sin' */
  LBG_Arctan_FOC_MATLABsin(T2, &ta);

  /* Sum: '<S31>/Add8' incorporates:
   *  Delay: '<S30>/Delay3'
   *  Product: '<S31>/Divide'
   *  Product: '<S31>/Divide1'
   *  Product: '<S31>/Product5'
   */
  rtb_sum_alpha = (0.0F - T1 / rtb_sum_alpha) - LBG_Arctan_FOC_DW.Delay3_DSTATE *
    ta / rtb_sum_alpha;

  /* DiscreteIntegrator: '<S82>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_d == 0) {
    /* DiscreteIntegrator: '<S82>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_f += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U_d;
  }

  /* End of DiscreteIntegrator: '<S82>/Integrator' */

  /* Sum: '<S92>/Sum' incorporates:
   *  Constant: '<S31>/Constant23'
   *  Product: '<S87>/PProd Out'
   */
  ta = rtb_sum_alpha * 2513.0F + LBG_Arctan_FOC_DW.Integrator_DSTATE_f;

  /* Switch: '<S90>/Switch2' incorporates:
   *  Constant: '<S31>/Constant25'
   *  Constant: '<S31>/Constant26'
   *  RelationalOperator: '<S90>/LowerRelop1'
   *  RelationalOperator: '<S90>/UpperRelop'
   *  Switch: '<S90>/Switch'
   */
  if (ta > 6283.18555F) {
    /* Switch: '<S90>/Switch2' */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U = 6283.18555F;
  } else if (ta < -6283.18555F) {
    /* Switch: '<S90>/Switch2' incorporates:
     *  Constant: '<S31>/Constant26'
     *  Switch: '<S90>/Switch'
     */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U = -6283.18555F;
  } else {
    /* Switch: '<S90>/Switch2' incorporates:
     *  Switch: '<S90>/Switch'
     */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U = ta;
  }

  /* End of Switch: '<S90>/Switch2' */

  /* Gain: '<S31>/Gain4' incorporates:
   *  Gain: '<S31>/Gain2'
   *  Gain: '<S31>/Gain3'
   *  Gain: '<S31>/f_2_we'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_g = 1.36418521F *
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U * 0.116666667F * 6.28318548F;

  /* Sum: '<S47>/Add1' incorporates:
   *  Constant: '<S47>/Filter_Constant'
   *  Constant: '<S47>/One'
   *  Product: '<S47>/Product'
   *  Product: '<S47>/Product1'
   */
  rt_Simulink_Struct.ob_spd = LBG_Arctan_FOC_DW.Delay_DSTATE_g * 0.02F + 0.98F *
    rt_Simulink_Struct.ob_spd;

  /* MATLAB Function: '<S31>/MATLAB Function' incorporates:
   *  Inport: '<Root>/Target_Speed'
   */
  if (arg_Target_Speed >= 0.0F) {
    T2 = 0.0F;
  } else {
    T2 = -3.14159274F;
  }

  /* Outputs for Atomic SubSystem: '<S31>/atan2' */
  /* Math: '<S31>/Math Function' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S41>/a16'
   *  Constant: '<S31>/Constant'
   *  Gain: '<S31>/Gain'
   *  MATLAB Function: '<S31>/MATLAB Function'
   *  Sum: '<S31>/Add'
   */
  rt_Simulink_Struct.ob_theta = rt_modf_snf(-LBG_Arctan_FOC_B.Merge_n + T2,
    6.28318548F);

  /* End of Outputs for SubSystem: '<S31>/atan2' */

  /* Switch: '<S75>/Switch' incorporates:
   *  Constant: '<S31>/Constant25'
   *  Constant: '<S31>/Constant26'
   *  RelationalOperator: '<S75>/u_GTE_up'
   *  RelationalOperator: '<S75>/u_GT_lo'
   *  Switch: '<S75>/Switch1'
   */
  if (ta >= 6283.18555F) {
    T2 = 6283.18555F;
  } else if (ta > -6283.18555F) {
    /* Switch: '<S75>/Switch1' */
    T2 = ta;
  } else {
    T2 = -6283.18555F;
  }

  /* Sum: '<S75>/Diff' incorporates:
   *  Switch: '<S75>/Switch'
   */
  T2 = ta - T2;

  /* Product: '<S79>/IProd Out' incorporates:
   *  Constant: '<S31>/Constant24'
   */
  rtb_sum_alpha *= 1.579136E+6F;

  /* Switch: '<S72>/Switch1' incorporates:
   *  Constant: '<S72>/Clamping_zero'
   *  Constant: '<S72>/Constant'
   *  Constant: '<S72>/Constant2'
   *  RelationalOperator: '<S72>/fix for DT propagation issue'
   */
  if (T2 > 0.0F) {
    rtPrevAction = 1;
  } else {
    rtPrevAction = -1;
  }

  /* Switch: '<S72>/Switch2' incorporates:
   *  Constant: '<S72>/Clamping_zero'
   *  Constant: '<S72>/Constant3'
   *  Constant: '<S72>/Constant4'
   *  RelationalOperator: '<S72>/fix for DT propagation issue1'
   */
  if (rtb_sum_alpha > 0.0F) {
    tmp = 1;
  } else {
    tmp = -1;
  }

  /* Switch: '<S72>/Switch' incorporates:
   *  Constant: '<S72>/Clamping_zero'
   *  Logic: '<S72>/AND3'
   *  RelationalOperator: '<S72>/Equal1'
   *  RelationalOperator: '<S72>/Relational Operator'
   *  Switch: '<S72>/Switch1'
   *  Switch: '<S72>/Switch2'
   */
  if ((T2 != 0.0F) && (rtPrevAction == tmp)) {
    /* Update for DiscreteIntegrator: '<S82>/Integrator' incorporates:
     *  Constant: '<S72>/Constant1'
     */
    LBG_Arctan_FOC_DW.Integrator_PREV_U_d = 0.0F;
  } else {
    /* Update for DiscreteIntegrator: '<S82>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_PREV_U_d = rtb_sum_alpha;
  }

  /* End of Switch: '<S72>/Switch' */

  /* Sum: '<S32>/Sum' incorporates:
   *  Delay: '<S11>/Delay1'
   *  Delay: '<S32>/Delay'
   *  Gain: '<S32>/Gain1'
   */
  T2 = LBG_Arctan_FOC_DW.Delay_DSTATE_f - 9.7E-5F *
    LBG_Arctan_FOC_DW.Delay1_DSTATE_c;

  /* Abs: '<S32>/Abs' */
  T1 = fabsf(T2);

  /* Sum: '<S32>/Sum1' incorporates:
   *  Delay: '<S11>/Delay3'
   *  Delay: '<S32>/Delay1'
   *  Gain: '<S32>/Gain3'
   */
  rtb_sum_alpha = LBG_Arctan_FOC_DW.Delay1_DSTATE_b - 9.7E-5F *
    LBG_Arctan_FOC_DW.Delay3_DSTATE_f;

  /* Abs: '<S32>/Abs1' */
  ta = fabsf(rtb_sum_alpha);

  /* Sum: '<S32>/Add3' incorporates:
   *  Math: '<S32>/Square1'
   *  Math: '<S32>/Square2'
   *  Sum: '<S32>/Add1'
   */
  ta = LBG_Arctan_FOC_ConstB.Square3 - (T1 * T1 + ta * ta);

  /* Sum: '<S32>/Add5' incorporates:
   *  Constant: '<S32>/Constant1'
   *  Delay: '<S11>/Delay1'
   *  Delay: '<S11>/Delay2'
   *  Delay: '<S32>/Delay'
   *  Gain: '<S32>/Gain'
   *  Gain: '<S32>/Gain4'
   *  Product: '<S32>/Product'
   *  Sum: '<S32>/Add'
   *  Sum: '<S32>/Add4'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_f += ((LBG_Arctan_FOC_DW.Delay2_DSTATE_a -
    0.194F * LBG_Arctan_FOC_DW.Delay1_DSTATE_c) + 1.0E+10F * T2 * ta) * 5.0E-5F;

  /* Sum: '<S32>/Add8' incorporates:
   *  Constant: '<S32>/Constant2'
   *  Delay: '<S11>/Delay3'
   *  Delay: '<S11>/Delay4'
   *  Delay: '<S32>/Delay1'
   *  Gain: '<S32>/Gain2'
   *  Gain: '<S32>/Gain7'
   *  Product: '<S32>/Product1'
   *  Sum: '<S32>/Add2'
   *  Sum: '<S32>/Add7'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE_b += (ta * rtb_sum_alpha * 1.0E+10F +
    (LBG_Arctan_FOC_DW.Delay4_DSTATE - 0.194F *
     LBG_Arctan_FOC_DW.Delay3_DSTATE_f)) * 5.0E-5F;

  /* DiscreteIntegrator: '<S33>/Discrete-Time Integrator' */
  if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_f == 0) {
    /* DiscreteIntegrator: '<S33>/Discrete-Time Integrator' */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_p += 5.0E-5F * (real32_T)
      elapsedTicks * LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_i;
  }

  /* End of DiscreteIntegrator: '<S33>/Discrete-Time Integrator' */

  /* Math: '<S33>/Mod' incorporates:
   *  Constant: '<S33>/Constant1'
   *  Delay: '<S3>/Delay'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_o = rt_modf_snf
    (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE_p, 6.28318548F);

  /* MATLAB Function: '<S33>/MATLAB sin' incorporates:
   *  Delay: '<S3>/Delay'
   */
  LBG_Arctan_FOC_MATLABsin(LBG_Arctan_FOC_DW.Delay_DSTATE_o, &ta);

  /* MATLAB Function: '<S33>/MATLAB cos' incorporates:
   *  Delay: '<S3>/Delay'
   */
  LBG_Arctan_FOC_MATLABcos(LBG_Arctan_FOC_DW.Delay_DSTATE_o, &T2);

  /* Sum: '<S33>/Add8' incorporates:
   *  Delay: '<S11>/Delay1'
   *  Delay: '<S11>/Delay3'
   *  Delay: '<S32>/Delay'
   *  Delay: '<S32>/Delay1'
   *  Gain: '<S32>/Gain5'
   *  Gain: '<S32>/Gain6'
   *  Gain: '<S32>/Gain8'
   *  Gain: '<S32>/Gain9'
   *  Product: '<S33>/Product4'
   *  Product: '<S33>/Product5'
   *  Sum: '<S32>/Add6'
   *  Sum: '<S32>/Add9'
   */
  ta = (LBG_Arctan_FOC_DW.Delay1_DSTATE_b - 9.7E-5F *
        LBG_Arctan_FOC_DW.Delay3_DSTATE_f) * 950.570312F * T2 -
    (LBG_Arctan_FOC_DW.Delay_DSTATE_f - 9.7E-5F *
     LBG_Arctan_FOC_DW.Delay1_DSTATE_c) * 950.570312F * ta;

  /* DiscreteIntegrator: '<S143>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_l == 0) {
    /* DiscreteIntegrator: '<S143>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_m += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U_b;
  }

  /* End of DiscreteIntegrator: '<S143>/Integrator' */

  /* Sum: '<S153>/Sum' incorporates:
   *  Constant: '<S33>/Constant23'
   *  Product: '<S148>/PProd Out'
   */
  rtb_sum_alpha = ta * 2513.0F + LBG_Arctan_FOC_DW.Integrator_DSTATE_m;

  /* Switch: '<S151>/Switch2' incorporates:
   *  Constant: '<S33>/Constant25'
   *  Constant: '<S33>/Constant26'
   *  RelationalOperator: '<S151>/LowerRelop1'
   *  RelationalOperator: '<S151>/UpperRelop'
   *  Switch: '<S151>/Switch'
   */
  if (rtb_sum_alpha > 6283.18555F) {
    /* Switch: '<S151>/Switch2' */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_i = 6283.18555F;
  } else if (rtb_sum_alpha < -6283.18555F) {
    /* Switch: '<S151>/Switch2' incorporates:
     *  Constant: '<S33>/Constant26'
     *  Switch: '<S151>/Switch'
     */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_i = -6283.18555F;
  } else {
    /* Switch: '<S151>/Switch2' incorporates:
     *  Switch: '<S151>/Switch'
     */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_i = rtb_sum_alpha;
  }

  /* End of Switch: '<S151>/Switch2' */

  /* Switch: '<S136>/Switch' incorporates:
   *  Constant: '<S33>/Constant25'
   *  Constant: '<S33>/Constant26'
   *  RelationalOperator: '<S136>/u_GTE_up'
   *  RelationalOperator: '<S136>/u_GT_lo'
   *  Switch: '<S136>/Switch1'
   */
  if (rtb_sum_alpha >= 6283.18555F) {
    T2 = 6283.18555F;
  } else if (rtb_sum_alpha > -6283.18555F) {
    /* Switch: '<S136>/Switch1' */
    T2 = rtb_sum_alpha;
  } else {
    T2 = -6283.18555F;
  }

  /* Sum: '<S136>/Diff' incorporates:
   *  Switch: '<S136>/Switch'
   */
  T2 = rtb_sum_alpha - T2;

  /* Product: '<S140>/IProd Out' incorporates:
   *  Constant: '<S33>/Constant24'
   */
  ta *= 1.579136E+6F;

  /* Switch: '<S133>/Switch1' incorporates:
   *  Constant: '<S133>/Clamping_zero'
   *  Constant: '<S133>/Constant'
   *  Constant: '<S133>/Constant2'
   *  RelationalOperator: '<S133>/fix for DT propagation issue'
   */
  if (T2 > 0.0F) {
    rtPrevAction = 1;
  } else {
    rtPrevAction = -1;
  }

  /* Switch: '<S133>/Switch2' incorporates:
   *  Constant: '<S133>/Clamping_zero'
   *  Constant: '<S133>/Constant3'
   *  Constant: '<S133>/Constant4'
   *  RelationalOperator: '<S133>/fix for DT propagation issue1'
   */
  if (ta > 0.0F) {
    tmp = 1;
  } else {
    tmp = -1;
  }

  /* Switch: '<S133>/Switch' incorporates:
   *  Constant: '<S133>/Clamping_zero'
   *  Logic: '<S133>/AND3'
   *  RelationalOperator: '<S133>/Equal1'
   *  RelationalOperator: '<S133>/Relational Operator'
   *  Switch: '<S133>/Switch1'
   *  Switch: '<S133>/Switch2'
   */
  if ((T2 != 0.0F) && (rtPrevAction == tmp)) {
    /* Update for DiscreteIntegrator: '<S143>/Integrator' incorporates:
     *  Constant: '<S133>/Constant1'
     */
    LBG_Arctan_FOC_DW.Integrator_PREV_U_b = 0.0F;
  } else {
    /* Update for DiscreteIntegrator: '<S143>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_PREV_U_b = ta;
  }

  /* End of Switch: '<S133>/Switch' */

  /* Gain: '<S375>/convert_pu' */
  rtb_sum_alpha = 0.159154937F * rt_Simulink_Struct.cal_theta;

  /* If: '<S375>/If' incorporates:
   *  Constant: '<S376>/Constant'
   *  RelationalOperator: '<S376>/Compare'
   */
  if (rtb_sum_alpha < 0.0F) {
    /* Outputs for IfAction SubSystem: '<S375>/If Action Subsystem' incorporates:
     *  ActionPort: '<S377>/Action Port'
     */
    /* DataTypeConversion: '<S377>/Convert_uint16' */
    T2 = floorf(rtb_sum_alpha);
    if (rtIsInfF(T2)) {
      T2 = 0.0F;
    } else {
      T2 = fmodf(T2, 65536.0F);
    }

    /* Sum: '<S377>/Sum' incorporates:
     *  DataTypeConversion: '<S377>/Convert_back'
     *  DataTypeConversion: '<S377>/Convert_uint16'
     */
    ta = rtb_sum_alpha - (real32_T)(T2 < 0.0F ? (int32_T)(int16_T)-(int16_T)
      (uint16_T)-T2 : (int32_T)(int16_T)(uint16_T)T2);

    /* End of Outputs for SubSystem: '<S375>/If Action Subsystem' */
  } else {
    /* Outputs for IfAction SubSystem: '<S375>/If Action Subsystem1' incorporates:
     *  ActionPort: '<S378>/Action Port'
     */
    /* DataTypeConversion: '<S378>/Convert_uint16' */
    T2 = truncf(rtb_sum_alpha);
    if (rtIsNaNF(T2) || rtIsInfF(T2)) {
      T2 = 0.0F;
    } else {
      T2 = fmodf(T2, 65536.0F);
    }

    /* Sum: '<S378>/Sum' incorporates:
     *  DataTypeConversion: '<S378>/Convert_back'
     *  DataTypeConversion: '<S378>/Convert_uint16'
     */
    ta = rtb_sum_alpha - (real32_T)(int16_T)(uint16_T)T2;

    /* End of Outputs for SubSystem: '<S375>/If Action Subsystem1' */
  }

  /* End of If: '<S375>/If' */

  /* Gain: '<S170>/indexing' */
  ta *= 800.0F;

  /* DataTypeConversion: '<S170>/Get_Integer' */
  T2 = truncf(ta);
  if (rtIsNaNF(T2) || rtIsInfF(T2)) {
    T2 = 0.0F;
  } else {
    T2 = fmodf(T2, 65536.0F);
  }

  rtb_Get_Integer = (uint16_T)(T2 < 0.0F ? (int32_T)(uint16_T)-(int16_T)
    (uint16_T)-T2 : (int32_T)(uint16_T)T2);

  /* End of DataTypeConversion: '<S170>/Get_Integer' */

  /* Sum: '<S170>/Sum2' incorporates:
   *  DataTypeConversion: '<S170>/Data Type Conversion1'
   */
  T1 = ta - (real32_T)rtb_Get_Integer;

  /* Selector: '<S170>/Lookup' incorporates:
   *  Constant: '<S170>/sine_table_values'
   *  Sum: '<S170>/Sum'
   */
  rtb_sum_alpha = LBG_Arctan_FOC_ConstP.sine_table_values_Value[rtb_Get_Integer];

  /* Sum: '<S374>/Sum4' incorporates:
   *  Constant: '<S170>/offset'
   *  Constant: '<S170>/sine_table_values'
   *  Product: '<S374>/Product'
   *  Selector: '<S170>/Lookup'
   *  Sum: '<S170>/Sum'
   *  Sum: '<S374>/Sum3'
   */
  rtb_sum_alpha += (LBG_Arctan_FOC_ConstP.sine_table_values_Value[(int32_T)
                    (rtb_Get_Integer + 1U)] - rtb_sum_alpha) * T1;

  /* Selector: '<S170>/Lookup' incorporates:
   *  Constant: '<S170>/offset'
   *  Constant: '<S170>/sine_table_values'
   *  Sum: '<S170>/Sum'
   *  Sum: '<S374>/Sum5'
   */
  ta = LBG_Arctan_FOC_ConstP.sine_table_values_Value[(int32_T)(rtb_Get_Integer +
    200U)];

  /* Sum: '<S374>/Sum6' incorporates:
   *  Constant: '<S170>/offset'
   *  Constant: '<S170>/sine_table_values'
   *  Product: '<S374>/Product1'
   *  Selector: '<S170>/Lookup'
   *  Sum: '<S170>/Sum'
   *  Sum: '<S374>/Sum5'
   */
  T1 = (LBG_Arctan_FOC_ConstP.sine_table_values_Value[(int32_T)(rtb_Get_Integer
         + 201U)] - ta) * T1 + ta;

  /* Outputs for Atomic SubSystem: '<S7>/Park Transform' */
  /* Sum: '<S7>/Sum' incorporates:
   *  Product: '<S169>/acos'
   *  Product: '<S169>/bsin'
   *  Sum: '<S169>/sum_Ds'
   */
  ta = rt_Simulink_Struct.cal_idref - (rtb_Sum_b * T1 + rtb_ibeta *
    rtb_sum_alpha);

  /* End of Outputs for SubSystem: '<S7>/Park Transform' */

  /* DiscreteIntegrator: '<S205>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_g == 0) {
    /* DiscreteIntegrator: '<S205>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_p += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U_c2;
  }

  /* End of DiscreteIntegrator: '<S205>/Integrator' */

  /* Sum: '<S214>/Sum' incorporates:
   *  Gain: '<S210>/Proportional Gain'
   */
  rtb_Saturation_a = 0.485F * ta + LBG_Arctan_FOC_DW.Integrator_DSTATE_p;

  /* Saturate: '<S212>/Saturation' */
  if (rtb_Saturation_a > 6.92820311F) {
    rtb_Saturation_a = 6.92820311F;
  } else if (rtb_Saturation_a < -6.92820311F) {
    rtb_Saturation_a = -6.92820311F;
  }

  /* End of Saturate: '<S212>/Saturation' */

  /* Outputs for Atomic SubSystem: '<S7>/Park Transform' */
  /* Sum: '<S7>/Sum1' incorporates:
   *  Product: '<S169>/asin'
   *  Product: '<S169>/bcos'
   *  Sum: '<S169>/sum_Qs'
   */
  T2 = rt_Simulink_Struct.cal_iqref - (rtb_ibeta * T1 - rtb_Sum_b *
    rtb_sum_alpha);

  /* End of Outputs for SubSystem: '<S7>/Park Transform' */

  /* DiscreteIntegrator: '<S253>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m == 0) {
    /* DiscreteIntegrator: '<S253>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_mi += 5.0E-5F * (real32_T)elapsedTicks *
      LBG_Arctan_FOC_DW.Integrator_PREV_U_b5;
  }

  /* End of DiscreteIntegrator: '<S253>/Integrator' */

  /* Sum: '<S262>/Sum' incorporates:
   *  Gain: '<S258>/Proportional Gain'
   */
  rtb_Saturation_h = 0.485F * T2 + LBG_Arctan_FOC_DW.Integrator_DSTATE_mi;

  /* Saturate: '<S260>/Saturation' */
  if (rtb_Saturation_h > 6.92820311F) {
    rtb_Saturation_h = 6.92820311F;
  } else if (rtb_Saturation_h < -6.92820311F) {
    rtb_Saturation_h = -6.92820311F;
  }

  /* End of Saturate: '<S260>/Saturation' */

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* AlgorithmDescriptorDelegate generated from: '<S166>/a16' incorporates:
   *  Product: '<S166>/dcos'
   *  Product: '<S166>/dsin'
   *  Product: '<S166>/qcos'
   *  Product: '<S166>/qsin'
   *  Sum: '<S166>/sum_alpha'
   *  Sum: '<S166>/sum_beta'
   */
  rtb_algDD_o1_m = rtb_Saturation_a * T1 - rtb_Saturation_h * rtb_sum_alpha;
  rtb_sum_alpha = rtb_Saturation_h * T1 + rtb_Saturation_a * rtb_sum_alpha;

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */
  /* Update for SwitchCase: '<S9>/Switch Case' */
  switch (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem) {
   case 0:
   case 2:
    break;

   case 1:
    /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
     *  ActionPort: '<S385>/Action Port'
     */
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV =
      LBG_Arctan_FOC_DW.FOC_PREV_T;
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = false;

    /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem1' */
    break;

   case 3:
    /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem3' incorporates:
     *  ActionPort: '<S387>/Action Port'
     */
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_PREV =
      LBG_Arctan_FOC_DW.FOC_PREV_T;
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem3_RESE = false;

    /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem3' */
    break;
  }

  /* Update for DiscreteIntegrator: '<S301>/Integrator' incorporates:
   *  Gain: '<S298>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE = 0U;
  LBG_Arctan_FOC_DW.Integrator_PREV_U = 970.0F * rtb_Sum2;

  /* Update for DiscreteIntegrator: '<S349>/Integrator' incorporates:
   *  Gain: '<S346>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_e = 0U;
  LBG_Arctan_FOC_DW.Integrator_PREV_U_c = 970.0F * rtb_Sum3;

  /* Update for UnitDelay: '<S23>/Unit Delay' incorporates:
   *  Delay: '<S10>/Delay'
   */
  LBG_Arctan_FOC_DW.UnitDelay_DSTATE = LBG_Arctan_FOC_DW.Delay_DSTATE_n;

  /* Update for Delay: '<S31>/Delay' incorporates:
   *  Delay: '<S30>/Delay2'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_l = LBG_Arctan_FOC_DW.Delay2_DSTATE;

  /* Update for Delay: '<S31>/Delay1' incorporates:
   *  Delay: '<S30>/Delay3'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE_j = LBG_Arctan_FOC_DW.Delay3_DSTATE;

  /* Update for DiscreteIntegrator: '<S31>/Discrete-Time Integrator' */
  LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E = 0U;

  /* Update for DiscreteIntegrator: '<S82>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_d = 0U;

  /* Update for Delay: '<S11>/Delay1' */
  LBG_Arctan_FOC_DW.Delay1_DSTATE_c = rtb_Sum_b;

  /* Update for Delay: '<S11>/Delay2' */
  LBG_Arctan_FOC_DW.Delay2_DSTATE_a = beta;

  /* Update for Delay: '<S11>/Delay3' */
  LBG_Arctan_FOC_DW.Delay3_DSTATE_f = rtb_ibeta;

  /* Update for Delay: '<S11>/Delay4' */
  LBG_Arctan_FOC_DW.Delay4_DSTATE = rtb_Vbeta;

  /* Update for DiscreteIntegrator: '<S33>/Discrete-Time Integrator' */
  LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_f = 0U;

  /* Update for DiscreteIntegrator: '<S143>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_l = 0U;

  /* Update for DiscreteIntegrator: '<S205>/Integrator' incorporates:
   *  Gain: '<S202>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_g = 0U;
  LBG_Arctan_FOC_DW.Integrator_PREV_U_c2 = 970.0F * ta;

  /* Update for DiscreteIntegrator: '<S253>/Integrator' incorporates:
   *  Gain: '<S250>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m = 0U;
  LBG_Arctan_FOC_DW.Integrator_PREV_U_b5 = 970.0F * T2;

  /* Outport: '<Root>/Vabc_PU' incorporates:
   *  Constant: '<S7>/Constant3'
   *  Gain: '<S7>/Gain1'
   *  Sum: '<S7>/Sum5'
   */
  arg_Vabc_PU[0] = 1.0F - 0.000244140625F * rtb_Saturation;
  arg_Vabc_PU[1] = 1.0F - 0.000244140625F * rtb_Saturation_l;
  arg_Vabc_PU[2] = 1.0F - 0.000244140625F * rtb_Tcmp3;

  /* Update for Delay: '<S1>/Delay' incorporates:
   *  Gain: '<S33>/Gain2'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE = 1.36418521F *
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U_i;

  /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator4' */

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 5.0E-5, which is the step size
   * of the task. Size of "clockTick0" ensures timer will not overflow during the
   * application lifespan selected.
   */
  LBG_Arctan_FOC_M->Timing.clockTick0++;
  if (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[1] == 0) {
    /* Update absolute timer for sample time: [0.001s, 0.0s] */
    /* The "clockTick1" counts the number of times the code of this task has
     * been executed. The resolution of this integer timer is 0.001, which is the step size
     * of the task. Size of "clockTick1" ensures timer will not overflow during the
     * application lifespan selected.
     */
    LBG_Arctan_FOC_M->Timing.clockTick1++;
  }

  if (LBG_Arctan_FOC_M->Timing.TaskCounters.TID[2] == 0) {
    /* Update absolute timer for sample time: [0.01s, 0.0s] */
    /* The "clockTick2" counts the number of times the code of this task has
     * been executed. The resolution of this integer timer is 0.01, which is the step size
     * of the task. Size of "clockTick2" ensures timer will not overflow during the
     * application lifespan selected.
     */
    LBG_Arctan_FOC_M->Timing.clockTick2++;
  }

  rate_scheduler();
}

/* Model initialize function */
void LBG_Arctan_FOC_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
   *  SubSystem: '<S1>/Speed_Control'
   */
  /* SystemInitialize for Enabled SubSystem: '<S4>/Subsystem' */
  /* SystemInitialize for SignalConversion generated from: '<S389>/Id_Ref' */
  LBG_Arctan_FOC_B.OutportBufferForId_Ref = LBG_Arctan_FOC_ConstB.Constant;

  /* End of SystemInitialize for SubSystem: '<S4>/Subsystem' */
  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' */
  /* Start for SwitchCase: '<S9>/Switch Case' */
  LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = -1;

  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator4' */

  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
   *  SubSystem: '<S1>/10ms_Task'
   */
  /* Enable for Chart: '<S2>/Chart' */
  LBG_Arctan_FOC_DW.previousTicks = LBG_Arctan_FOC_M->Timing.clockTick2;

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' */

  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T = true;

  /* Enable for DiscreteIntegrator: '<S301>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE = 1U;

  /* Enable for DiscreteIntegrator: '<S349>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_e = 1U;

  /* Enable for DiscreteIntegrator: '<S31>/Discrete-Time Integrator' */
  LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E = 1U;

  /* Enable for DiscreteIntegrator: '<S82>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_d = 1U;

  /* Enable for DiscreteIntegrator: '<S33>/Discrete-Time Integrator' */
  LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_f = 1U;

  /* Enable for DiscreteIntegrator: '<S143>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_l = 1U;

  /* Enable for DiscreteIntegrator: '<S205>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_g = 1U;

  /* Enable for DiscreteIntegrator: '<S253>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m = 1U;

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' */
}

/* Model terminate function */
void LBG_Arctan_FOC_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
